package com.lanyou.esb.entity;

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.lanyou.cook.common.DefaultDateJsonSerializer;

@Entity
@EntityListeners({ AuditingEntityListener.class})
@Table(name = "T_ESB_BU_IF_FTP_LOG")//还没有增加监听
public class FtpLog {

	private String id;//FTP接口执行日志表ID
	
	private String sourceAddress;//源主机地址
	
	private String targetAddress;//目标主机地址
	
	private String fileName;//文件名称
	
	private String fileSize;//文件大小
	
	private Date taransportDate;//文件传输开始时间
	
	private String transportResult;//传输结果
	
	private String transportResultDes;//传输结果描述
	
	private Ftp ftp;
	
	private String ifName;
	
	private String isEnable;
	
	private String creator;

	private String modifier;

	private Date createTime;

	private Date modifyTime;
	
	
	public final static Specification<FtpLog> EmptySpecification = new Specification<FtpLog>(){
		@Override
		public Predicate toPredicate(Root<FtpLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
			return null;
		}
	};
	
	public static Specification<FtpLog> idSpecification(String id){
		return new Specification<FtpLog>(){
			@Override
			public Predicate toPredicate(Root<FtpLog> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				query.where(cb.equal(root.get("id"), id));
				return null;
			}
		};
	}
	
	
	
	@Id
	@GeneratedValue(generator = "uuidGenerator")
	@GenericGenerator(name = "uuidGenerator", strategy = "uuid")
	@Column(name = "FTP_LOG_ID", length = 36)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	@Column(name="SOURCE_ADDRESS")
	public String getSourceAddress() {
		return sourceAddress;
	}

	public void setSourceAddress(String sourceAddress) {
		this.sourceAddress = sourceAddress;
	}

	@Column(name="TARGET_ADDRESS")
	public String getTargetAddress() {
		return targetAddress;
	}


	public void setTargetAddress(String targetAddress) {
		this.targetAddress = targetAddress;
	}

	@Column(name="FILE_NAME")
	public String getFileName() {
		return fileName;
	}


	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	@Column(name="FILE_SIZE")
	public String getFileSize() {
		return fileSize;
	}


	public void setFileSize(String fileSize) {
		this.fileSize = fileSize;
	}

	@Column(name="TRANSPORT_DATE")
	public Date getTaransportDate() {
		return taransportDate;
	}


	public void setTaransportDate(Date taransportDate) {
		this.taransportDate = taransportDate;
	}

	@Column(name="TRANSPORT_RESULT")
	public String getTransportResult() {
		return transportResult;
	}


	public void setTransportResult(String transportResult) {
		this.transportResult = transportResult;
	}

	@Column(name="TRANSPORT_RESULT_DES")
	public String getTransportResultDes() {
		return transportResultDes;
	}


	public void setTransportResultDes(String transportResultDes) {
		this.transportResultDes = transportResultDes;
	}

	@ManyToOne
	@JoinColumn(name = "IF_FTP_ID")
	public Ftp getFtp() {
		return ftp;
	}

	
	public void setFtp(Ftp ftp) {
		this.ftp = ftp;
	}


	@Column(name = "IF_NAME", length = 50)
	public String getIfName() {
		return ifName;
	}



	public void setIfName(String ifName) {
		this.ifName = ifName;
	}



	@Column(name = "IS_ENABLE", length = 2)
	public String getIsEnable() {
		return isEnable;
	}


	public void setIsEnable(String isEnable) {
		this.isEnable = isEnable;
	}

	
	

	@CreatedBy
	@Column(length = 50, updatable=false)
	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	@LastModifiedBy
	@Column(length = 50)
	public String getModifier() {
		return modifier;
	}

	public void setModifier(String modifier) {
		this.modifier = modifier;
	}

	@Column(name = "CREATED_DATE", updatable=false)
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@CreatedDate
	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	@Column(name = "LAST_UPDATED_DATE")
	@JsonSerialize(using = DefaultDateJsonSerializer.class)
	@LastModifiedDate
	public Date getModifyTime() {
		return modifyTime;
	}

	public void setModifyTime(Date modifyTime) {
		this.modifyTime = modifyTime;
	}
	
	@Transient
	public String getIfNameForDisplay() {
		return getFtp().getInterfaceName();
	}
	
	@Transient
	public String getTransportDateForDisplay() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getTaransportDate());
	}
	
	@Transient
	public String getTransportResultForDisplay() {

		if(StringUtils.equals(getTransportResult(), "0")){
			return new String("失败");
		}else{
			return new String("成功");
		}
	}
}
